//
//  WTFDYHDLeftList.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/18.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "WTFDYHDLeftList.h"
#import "SelectedFenZuAlertView.h"
#define CELLH 90*fh
@interface WTFDYHDLeftList ()
@property (nonatomic, strong) NSMutableArray *dataListArr;

@property (nonatomic, strong) SelectedFenZuAlertView *fenZuAlertView;
@property (nonatomic, copy) NSString *selectId;//选中分组id
@property (nonatomic, strong) SYNoLabel *nolab;


@end

@implementation WTFDYHDLeftList

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[WTFDTLCell class] forCellReuseIdentifier:@"WTFDTLCell"];
    self.tableView.tableFooterView = [UIView new];
//    _headerSearchView = [[WTFDSearchView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
//    self.tableView.tableHeaderView =_headerSearchView;
    _nolab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self.view];
    self.tableView.tableFooterView = [UIView new];
    [_nolab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(100);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.width.mas_equalTo(320);
        make.height.mas_equalTo(540);
    }];
}
- (void)chuanrushujuDataListArr:(NSMutableArray *)datalistArr {
    if (datalistArr.count == 0) {
        self.nolab.hidden = NO;
    } else {
        self.nolab.hidden = YES;
    }
    
    self.dataListArr = datalistArr;
    [self.tableView reloadData];
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataListArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CELLH;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WTFDTLCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WTFDTLCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[WTFDTLCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WTFDTLCell"];
    }
    cell.indexPath = indexPath;
    __weak typeof(self) weakSelf = self;
    SYWenTiFuDaoModel *wtfd = self.dataListArr[indexPath.row];
    cell.mainBlock = ^(NSInteger index) {
        if (index == 1) { //分组
            [weakSelf requestDataSort];
            weakSelf.selectId = wtfd.questionId;
        } else {//删除
            [weakSelf requestDeleteId:wtfd.questionId];
        }
    };
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.doteImageView.hidden = YES;
    
    
    [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:wtfd.headIng] placeholderImage:[UIImage imageNamed:@""]];
    cell.nameLabel.text = wtfd.name;
    cell.timeLabel.text = wtfd.createDate;
    cell.detailLabel.text = wtfd.content;
    
    return cell;
}

//请求分组
- (void)requestDataSort {//self.mainBlock(1);
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    [SYRequestAPI fd_requestFenZuQuestionSortKeyWord:@"" success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        [weakSelf alertFenZuSelectedViewListArr:listArr];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        
    }];
}

- (void)alertFenZuSelectedViewListArr:(NSMutableArray *)listArr {
    _fenZuAlertView = [[SelectedFenZuAlertView alloc]init];
    _fenZuAlertView.dataArray = listArr;
    _fenZuAlertView.preferredContentSize = CGSizeMake(360, 300);
    _fenZuAlertView.view.backgroundColor = [UIColor whiteColor];
    _fenZuAlertView.popoverPresentationController.sourceView = self.view;
    _fenZuAlertView .modalPresentationStyle = UIModalPresentationPopover;
    _fenZuAlertView.popoverPresentationController.sourceView = self.view;
    _fenZuAlertView.popoverPresentationController.sourceRect = CGRectMake(350, 180, 0, 0);
    _fenZuAlertView.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    [self presentViewController:_fenZuAlertView animated:YES completion:nil];
    __weak typeof(self) weakSelf = self;
    _fenZuAlertView.block = ^(NSInteger index) {
        //得到选择的当前班级
        [weakSelf requestDataFenZuId:index];
    };
}

//分组
- (void)requestDataFenZuId:(NSInteger)index {
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI fd_requestQuestionSortQuestionId:self.selectId sortId:SJStringWithFormat(index) oldSortId:@"" success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:@"添加分组成功"];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:error.message];
    }];
    
}




- (void)requestDeleteId:(NSString *)proId {
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    [SYRequestAPI fd_requestQuestionDeleteQuestionId:proId success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        [MBProgressHUD zr_showError:@"删除成功"];
        if (self.wtBlock) {
            self.wtBlock(1);
        }
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        [MBProgressHUD zr_showError:error.message];
    }];
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}



// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SYWenTiFuDaoModel *wtfd = self.dataListArr[indexPath.row];
    if (self.block) {
        self.block(wtfd, @"");
    }
    
    
}


// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    _headerSearchView = [[WTFDSearchView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
    _headerSearchView.backgroundColor = [UIColor whiteColor];
    return _headerSearchView;
}

@end
